package georah.map.layers
{

    /**
     *
     * @author yuanm
     */
    public final class GraphicLayer extends FeatureLayer
    {
        private var _graphics:Array=[];

		private var _prototype:MapGraphic=new SimpleMarker();

		public final function graphicN(n:int):MapGraphic
		{
			return _graphics[n];
		}

		public final function get numGraphics():int
		{
			return _graphics.length;
		}

		public final function set prototype(value:MapGraphic):void
		{
			_prototype=value;
		}

		override protected function map_changeHandler(event:MapEvent):void
		{
			super.map_changeHandler(event);

			resize(provider.numFeatures);

			var pixel:Point=null;
			var marker:MapGraphic=null;

			for (var i:int=0; i < provider.numFeatures; i++)
			{
				pixel=map.fromWhereToLayersPixel(OGCPoint(provider.shapeN(i)));
				marker=MapGraphic(_graphics[i]);

				if (!contains(marker))
				{
					addChild(marker);
				}

				marker.x=pixel.x;
				marker.y=pixel.y;

				if (_prototype.width <= 0)
				{
					marker.width=32.0;
				}

				if (_prototype.height <= 0)
				{
					marker.height=32.0;
				}
			}
		}

		private final function resize(size:int):void
		{
			if (size < 0)
			{
				return;
			}

			var delta:int=size - _graphics.length;

			if (delta > 0)
			{
				for (var ia:int=0; ia < delta; ia++)
				{
					_graphics.push(_prototype.clone());
				}
			}

			if (delta < 0)
			{
				for (var ir:int=0; ir < -delta; ir++)
				{
					delete _graphics[_graphics.length - 1];
					_graphics.pop();
				}
			}
		}

    }
}
